package com.livew.framework.base.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.livew.framework.base.dao.BaseMapper;
import com.livew.framework.base.dao.FileMapper;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.service.FileService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.utils.XTCurrentUser;
import com.livew.framework.utils.XTListUtils;
import com.livew.framework.utils.XTStringUtils;

@Service
public  class FileServiceImpl extends BaseServiceImpl<FileEO> implements FileService{

	@Resource
	private FileMapper fileMapper;
	
	@Resource
	private HttpSession session;
	
	public FileServiceImpl(BaseMapper<FileEO> baseMapper) {
		super(baseMapper);
	}

	@Override
	public void saveFiles(List<FileEO> files) throws XTBusinessException{
		if(XTListUtils.isEmpty(files)) {
			throw new XTBusinessException("参数错误");
		}
		for (FileEO file : files) {
			file.setCreateTime(new Date());
			file.setCreateUserId(XTCurrentUser.getCurrentUserId(session));
		}
		fileMapper.insertFilesSelective(files);
	}

	@Override
	public void updateBySrcName(FileEO file) throws XTBusinessException {
		if(file==null||XTStringUtils.isEmpty(file.getSrcname())){
			throw new XTBusinessException("参数错误");
		}
		if(XTStringUtils.isEmpty(file.getCaseClazz())||file.getCaseId()==null){
			throw new XTBusinessException("参数错误");
		}
		file.setUpdateTime(new Date());
		file.setUpdateUserId(XTCurrentUser.getCurrentUserId(session));
		fileMapper.delBySrcName(file);
		fileMapper.updateBySrcName(file);
	}

	@Override
	public void delBySrcName(FileEO file) throws XTBusinessException {
		if(file==null||XTStringUtils.isEmpty(file.getSrcname())){
			throw new XTBusinessException("参数错误");
		}
		List<FileEO> files =	fileMapper.selectListByCondition(file);
		if(XTListUtils.isEmpty(files)){
			throw new XTBusinessException("参数错误");
		}
		file.setUpdateTime(new Date());
		file.setUpdateUserId(XTCurrentUser.getCurrentUserId(session));
		fileMapper.delFileBySrcName(file);
	}

	@Override
	public Map<Long,List<FileEO>> getListToMap(List<Long> caseIds, String caseClazz, String type) {
		Map<Long,List<FileEO>>  map = new HashMap<Long, List<FileEO>>();
		if(XTListUtils.isEmpty(caseIds)||XTStringUtils.isEmpty(caseClazz)) {
			return map;
		}
		FileEO file = new FileEO();
		file.setCaseClazz(caseClazz);
		file.setCaseIds(caseIds);
		file.setType(type);
		List<FileEO> files =  fileMapper.selectListByByCaseIds(file);
		if(!XTListUtils.isEmpty(files)) {
			List<FileEO> list = null;
			for (FileEO file2 : files) {
				List<FileEO> tempList =	map.get(file2.getCaseId());
				if(XTListUtils.isEmpty(tempList)){//第一次添加
					list = new ArrayList<>();
					list.add(file2);
					map.put(file2.getId(), list);
				}else{
					tempList.add(file2);
				}
			}
		}
		return map;
	}
	
	
	@Override
	public List<FileEO> getList(List<Long> caseIds, String caseClazz, String type) {
		List<FileEO> list = null;
		if(XTListUtils.isEmpty(caseIds)||XTStringUtils.isEmpty(caseClazz)) {
			return list;
		}
		FileEO file = new FileEO();
		file.setCaseClazz(caseClazz);
		file.setCaseIds(caseIds);
		file.setType(type);
		List<FileEO> files =  fileMapper.selectListByByCaseIds(file);
		return files;
	}

	@Override
	public Map<Long,FileEO> getMapTopOne(List<Long> caseIds, String caseClazz, String type) {
		Map<Long,FileEO>  map = new HashMap<Long, FileEO>();
		if(XTListUtils.isEmpty(caseIds)||XTStringUtils.isEmpty(caseClazz)) {
			return map;
		}
		FileEO file = new FileEO();
		file.setCaseClazz(caseClazz);
		file.setCaseIds(caseIds);
		file.setType(type);
		List<FileEO> files =  fileMapper.selectListByByCaseIds(file);
		if(!XTListUtils.isEmpty(files)) {
			for (FileEO file2 : files) {
				FileEO temp =	map.get(file2.getCaseId());
				if(temp==null){//第一次添加
					map.put(file2.getCaseId(), file2);
			}
			}
		}
		return map;
	}
	
}
